package cn.iocoder.yudao.module.member.controller.app.user;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.module.member.controller.app.user.vo.*;
import cn.iocoder.yudao.module.member.convert.user.MemberUserConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.level.MemberLevelDO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.service.level.MemberLevelService;
import cn.iocoder.yudao.module.member.service.user.MemberUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;


import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "用户 APP - 用户个人中心")
@RestController
@RequestMapping("/member/user")
@Validated
@Slf4j
public class AppMemberUserController {

    @Resource
    private MemberUserService userService;
    @Resource
    private MemberLevelService levelService;
    @GetMapping("/get")
    @Operation(summary = "获得患者基本信息")
    @PreAuthenticated
    public CommonResult<AppMemberUserInfoRespVO> getUserInfo() {
        MemberUserDO user = userService.getUser(getLoginUserId());
        MemberLevelDO level = levelService.getLevel(user.getLevelId());
        AppMemberUserInfoRespVO convert = MemberUserConvert.INSTANCE.convert(user, level);
        return success(convert);
    }

    @GetMapping("/getDockerLoginInfo")
    @Operation(summary = "获得登录医生基本信息")
    @PreAuthenticated
    public CommonResult<MemberUserDO> getDockerLoginInfo() {
        MemberUserDO user = userService.getDockerLoginInfo(getLoginUserId());
        return success(user);
    }

    @GetMapping("/getDockerInfo")
    @Operation(summary = "获得列表医生详细基本信息")
    @PreAuthenticated
    public CommonResult<MemberUserDO> getDockerInfo(@RequestParam("id") Long id) {
        MemberUserDO user = userService.getUser(id);
        return success(user);
    }

    @GetMapping("/updatePop")
    @Operation(summary = "弹框提醒")
    @PreAuthenticated
    public CommonResult<Boolean> updatePop() {
        userService.updatePop(getLoginUserId());
        return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "修改患者基本信息")
    @PreAuthenticated
    public CommonResult<Boolean> updateUser(@RequestBody @Valid AppMemberUserUpdateReqVO reqVO) {
        userService.updateUser(getLoginUserId(), reqVO);
        return success(true);
    }

    @PutMapping("/updateDoctor")
    @Operation(summary = "修改医生详细信息")
    @PreAuthenticated
    public CommonResult<Boolean> updateDoctor(@RequestBody @Valid AppMemberDoctorUpdateReqVO reqVO) {
        userService.updateDoctor(reqVO);
        return success(true);
    }

    @PutMapping("/update-mobile")
    @Operation(summary = "修改用户手机")
    @PreAuthenticated
    public CommonResult<Boolean> updateUserMobile(@RequestBody @Valid AppMemberUserUpdateMobileReqVO reqVO) {
        userService.updateUserMobile(getLoginUserId(), reqVO);
        return success(true);
    }

    @PutMapping("/update-password")
    @Operation(summary = "修改用户密码", description = "用户修改密码时使用")
    @PreAuthenticated
    public CommonResult<Boolean> updatePassword(@RequestBody @Valid AppMemberUserUpdatePasswordReqVO reqVO) {
        userService.updateUserPassword(getLoginUserId(), reqVO);
        return success(true);
    }

    @PutMapping("/reset-password")
    @Operation(summary = "重置密码", description = "用户忘记密码时使用")
    public CommonResult<Boolean> resetPassword(@RequestBody @Valid AppMemberUserResetPasswordReqVO reqVO) {
        userService.resetUserPassword(reqVO);
        return success(true);
    }

    /**
     * 医生资质认证/实名认证
     * @param reqVO
     * @return
     */
    @PostMapping("/update-authentication")
    @Operation(summary = "医生资质认证/实名认证", description = "资质认证")
    public CommonResult<Boolean> updateAuthentication(@RequestBody @Valid AppMemberUserUpdateAuthenticationReqVO reqVO) {
        userService.updateAuthentication(reqVO);
        return success(true);
    }

    /**
     * 统计数量++
     * @param
     * @return
     */
    @GetMapping("/updateNum")
    @Operation(summary = "统计数量++", description = "")
    @Parameter(name = "field", description = "患者数量:‘patientNum’;开方数量:‘extractionNum’;\"咨询数量:‘consultNum’;\"", required = false, example = "1")
    public CommonResult<Integer> updateNum(@RequestParam("field") String  field) {
        int count = userService.updateNum(field);
        return success(count);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "注销用户")
    public CommonResult<Boolean> deleteUser() {
        userService.deleteUserLogin();
        return success(true);
    }

}

